package com.mofang.powerdekorhelper.activity.product;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.persenter.ProductDetailPersenter;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.utils.library.GradationScrollView;
import com.mofang.powerdekorhelper.view.ProductDetailView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class ProductDetailActivity extends MvpActivity<ProductDetailView, ProductDetailPersenter> implements
        ProductDetailView,
        GradationScrollView.ScrollViewListener,
        TabLayout.OnTabSelectedListener {

    @BindView(R.id.product_detail_scrollview)
    GradationScrollView scrollview;

    @BindView(R.id.product_detail_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.product_top_img)
    ImageView productImv;

    @BindView(R.id.product_detail_tab)
    TabLayout productDetailTab;

    private int height;
    private int width;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_product_detail_layout);
        initTitleBarWithback(mTitleBar, R.string.product_detail);
        mTitleBar.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));
        mTitleBar.setTitleColor(Color.argb((int) 0, 255, 255, 255));
        scrollview.setScrollViewListener(this);
    }

    @Override
    public void initData() {
        height = productImv.getHeight();
        initDetailTab();
        initListeners();
    }

    private void initListeners() {

        ViewTreeObserver vto = mTitleBar.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mTitleBar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                height = mTitleBar.getHeight() - 10;

                scrollview.setScrollViewListener(ProductDetailActivity.this);
            }
        });
    }

    private void initDetailTab() {
        productDetailTab.addTab(productDetailTab.newTab().setText("图文详情").setTag(1));
        productDetailTab.addTab(productDetailTab.newTab().setText("案例展示").setTag(2));
        productDetailTab.addTab(productDetailTab.newTab().setText("产品参数").setTag(3));
        productDetailTab.setOnTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        Integer flag = (Integer) tab.getTag();
        switch (flag) {

            case 1:
                toast("图文详情");
                break;
            case 2:
                toast("案例展示");
                break;
            case 3:
                toast("产品参数");
                break;


        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {

        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

    @Override
    public ProductDetailPersenter initPresenter() {
        return new ProductDetailPersenter();
    }

    @Override
    public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {   //设置标题的背景颜色
            mTitleBar.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));
            mTitleBar.setTitleColor(Color.argb((int) 0, 255, 255, 255));
        } else if (y > 0 && y <= height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / height;
            float alpha = (255 * scale);
            mTitleBar.setTitleColor(Color.argb((int) alpha, 1, 24, 28));
            mTitleBar.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
        } else {    //滑动到banner下面设置普通颜色
            mTitleBar.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
        }
    }

}
